﻿/*
 * This file is part of CommunitySettlers.
 *
 * CommunitySettlers is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * CommunitySettlers is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with CommunitySettlers.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Christoph Husse
 * 
 * Also checkout our homepage: http://communitysettlers.codeplex.com/
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace SettlersEngine
{
    internal partial class RuntimeGrid
    {

        public List<Point> FindSpacesForBuilding(IBuildableObject inBuildable, Rectangle inScreen)
        {
            List<Point> result = new List<Point>();

            for (int screenX = inScreen.X; screenX < inScreen.X + inScreen.Width; screenX++)
            {
                for (int screenY = inScreen.Y; screenY < inScreen.Y + inScreen.Height; screenY++)
                {
                    if (CanPlaceObject(inBuildable, new Point(screenX, screenY)))
                    {
                        result.Add(new Point(screenX + (int)(inBuildable.LockedBounds.Width / 2),
                            screenY + (int)(inBuildable.LockedBounds.Height / 2)));
                    }
                }
            }

            return result;
        }

        public void EnumResourceProviders(Point inAround, Func<IResourceObject, WalkResult> inHandler)
        {
            m_ResourceProvs.EnumAround(inAround, inHandler);
        }

        public void EnumSettlers(Point inAround, Func<IMovableObject, WalkResult> inHandler)
        {
            m_Settlers.EnumAround(inAround, inHandler);
        }

        /// <summary>
        /// If a resource is placed, it shouldn't fail if the desired position is already in use.
        /// Instead the resource should drop at the nearest possible position around the target and
        /// this is what this method searches for.
        /// </summary>
        /// <param name="inStartPos"></param>
        /// <returns></returns>
        internal bool FindFreeSpaceForResource(Point inStartPos, out Point outFreeSpace)
        {
            Point result = new Point();

            outFreeSpace = result;

            // try to find standard free space
            if (WalkResult.Success != GridSearch.GridWalkAround(inStartPos, Width, Height, (current) =>
            {
                if (this[current].FixedObject != null)
                    return WalkResult.NotFound;

                // we found a free spot
                result = current;

                return WalkResult.Success;
            }))
            {
                // no free spot found
                return false;
            }

            outFreeSpace = result;

            return true;
        }
    }
}
